package com.dq.utils;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by 葛文镇
 * email:15258397904@163.com
 */
public class BeanNullUtil {
    /**
     * 获取 null 值进行忽略
     * @param o
     * @return  自动创建bean的工具类??
     */
    public static String[] getNullPropertyNames (Object o) {
        final BeanWrapper src = new BeanWrapperImpl(o);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }else {
                // 忽略 创建时间,避免 修改对象 对 createTime进行重新赋值
                if("createTime".equals(pd.getName())) {
                    emptyNames.add(pd.getName());
                }
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
}
